package com.integral.util;

import java.security.AlgorithmParameters;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.Security;
import java.util.Random;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

import org.bouncycastle.jce.provider.BouncyCastleProvider;

/**
 * @Description:
 * @Date: 2018/4/8
 * @Author: wcf
 */
public class StringUtils extends org.apache.commons.lang3.StringUtils{
	
	public static boolean initialized = false;   
	
    /**
     * StringUtils工具类方法
     * 获取一定长度的随机字符串，范围0-9，a-z
     * @param length：指定字符串长度
     * @return 一定长度的随机字符串
     */
    public static String getRandomStringByLength(int length) {
        String base = "abcdefghijklmnopqrstuvwxyz0123456789";
        Random random = new Random();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < length; i++) {
            int number = random.nextInt(base.length());
            sb.append(base.charAt(number));
        }
        return sb.toString();
    }
    
    
    /**  
     * AES解密  
     * @param content 密文  
     * @return  
     * @throws InvalidAlgorithmParameterException   
     * @throws NoSuchProviderException   
     */    
    public static byte[] decrypt(byte[] content, byte[] keyByte, byte[] ivByte) throws InvalidAlgorithmParameterException {    
        initialize();    
        try {    
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding");    
            Key sKeySpec = new SecretKeySpec(keyByte, "AES");    
                
            cipher.init(Cipher.DECRYPT_MODE, sKeySpec, generateIV(ivByte));// 初始化     
            byte[] result = cipher.doFinal(content);    
            return result;    
        } catch (NoSuchAlgorithmException e) {    
            e.printStackTrace();      
        } catch (NoSuchPaddingException e) {    
            e.printStackTrace();      
        } catch (InvalidKeyException e) {    
            e.printStackTrace();    
        } catch (IllegalBlockSizeException e) {    
            e.printStackTrace();    
        } catch (BadPaddingException e) {    
            e.printStackTrace();    
        } catch (NoSuchProviderException e) {    
            // TODO Auto-generated catch block    
            e.printStackTrace();    
        } catch (Exception e) {    
            // TODO Auto-generated catch block    
            e.printStackTrace();    
        }    
        return null;    
    }      
        
    public static void initialize(){      
        if (initialized) return;      
        Security.addProvider(new BouncyCastleProvider());      
        initialized = true;      
    }    
    //生成iv      
    public static AlgorithmParameters generateIV(byte[] iv) throws Exception{      
        AlgorithmParameters params = AlgorithmParameters.getInstance("AES");      
        params.init(new IvParameterSpec(iv));      
        return params;      
    } 
}
